﻿using System;
using System.IO;
using System.Reflection;
using DotNetNuke.Entities.Tabs;

namespace HeroldIT.Dnn.SkinObjects
{
    /// <summary>
    /// Localizes various aspects of the DotNetNuke framework using Apollo MLAPI if available.
    /// </summary>
    public static class Localizer
    {
        #region static properties

        /// <summary>
        /// Holds the lock for initializing the <see cref="tabLocalizer"/> object.
        /// </summary>
        private static readonly object tabLocalizerInitializeLock = new object();

        /// <summary>
        /// Holds whether the tabLocalizer has been initialized.
        /// </summary>
        private static bool tabLocalizerInitialized;

        /// <summary>
        /// Holds the LocalizeTab object for localizing tabs.
        /// </summary>
        private static object tabLocalizer;

        /// <summary>
        /// Gets the LocalizeTab object for localizing tabs.
        /// </summary>
        private static object TabLocalizer
        {
            get
            {
                if (!tabLocalizerInitialized && null == tabLocalizer)
                {
                    lock (tabLocalizerInitializeLock)
                    {
                        if (null == tabLocalizer)
                        {
                            try
                            {
                                tabLocalizer = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(
                                    "Apollo.LocalizationApi", "Apollo.DNN_Localization.LocalizeTab");
                            }
                            catch (FileNotFoundException)
                            {
                            }
                            catch (TypeLoadException)
                            {
                            }
                            tabLocalizerInitialized = true;
                        }
                    }
                }
                return tabLocalizer;
            }
        }

        #endregion

        #region static methods

        /// <summary>
        /// Localizes the given <see cref="TabInfo"/> using the Apollo framework.
        /// </summary>
        /// <remarks>If the Apollo framework is not installed, the original <see cref="TabInfo"/> is returned.</remarks>
        /// <param name="tab">The <see cref="TabInfo"/> to localize.</param>
        /// <returns>The localized <see cref="TabInfo"/>.</returns>
        public static TabInfo LocalizeTab(TabInfo tab)
        {
            if (null == TabLocalizer)
            {
                return tab;
            }
            MethodInfo method = TabLocalizer.GetType().GetMethod("getLocalizedTab", new[] {typeof (TabInfo)});
            if (null == method)
            {
                return tab;
            }

            TabInfo localizedTab = method.Invoke(TabLocalizer, new[] {tab}) as TabInfo;
            return localizedTab;
        }

        #endregion
    }
}